<?php namespace app\home\controller;

class Api {
	public function handler() {
        $message = Wx::getMessage();
//        //文本消息
        if ( Wx::instance( 'message' )->isText() ) {
            //让有消息处理功能的模块来处理一下(非关键词)
            $this->handlerMessage();
            $this->text($message->Content);
        }
        //菜单点击事件
        if ( Wx::instance( 'message' )->isClick() ) {
            $content = $message->EventKey;
            $this->text($content);
        }
        //没有回复内容时的,默认的回复
        Wx::instance( 'message' )->text( v( 'config.default_message' ) );
	}

	protected function handlerMessage(){
        $res = Db::table('plugin')->where('is_handler',1)->get();
        if($res){
            foreach($res as $r){
                $class = 'module\\'.$r['name'].'\Handler';
                if(class_exists($class)){
                    $obj =new $class;
                    $obj->run();
                }
            }
        }
    }


//	公共回复
    protected function text($content){
        $keyword = Db::table( 'keywords' )->where( 'keyword', $content )->first();
        if ( $keyword ) {
            //有关键词,启动相应模块进行响应
            $class = 'module\\' . $keyword['module'] . '\Process';
            ( new $class() )->handler( $keyword['kid'] );
            exit;
        }
    }





	public function butten(){
        $json=<<<str
{
     "button":[
     {	
          "type":"click",
          "name":"今日歌曲",
          "key":"V1001_TODAY_MUSIC"
      },
      {
           "name":"菜单",
           "sub_button":[
           {	
               "type":"view",
               "name":"搜索",
               "url":"http://www.soso.com/"
            },
            {
               "type":"view",
               "name":"视频",
               "url":"http://v.qq.com/"
            },
            {
               "type":"click",
               "name":"赞一下我们",
               "key":"V1001_GOOD"
            }]
       }]
 }
str;
        $res = Wx::instance('button')->create($json);
        dd($res);
    }



}